
/**

 @Name：layuiAdmin 默认部分 data 初始化
 @Author：flc
 @Site：http://www.layui.com/admin/
 @License: LPPL

 */

layui.define(['jquery','element'], function(exports) {
    var $ = layui.jquery,
        element = layui.element,
        initData = {
            publicquery:{
                csslink: function(props) {
                    var link = document.createElement('link');
                    document.body.appendChild(link);
                    link .setAttribute('rel','stylesheet');
                    link .setAttribute('type','text/css');
                    link .setAttribute('href',layui.cache.base+'../style/theme/menu' + props.type+'.css');
                },
                props:{
                    navLeftName:'#LAY-system-side-menu',
                    navTopName:'.topNavigation',
                    musterTopName:'lay-top-nav'
                }
            },
            navInit : function(props) {
                var _this = this;
                // menu  data  init
                layui.use(['admin'], function(){
                    var admin=layui.admin;
                    var path =  layui.router().path
                        ,pathURL = layui.admin.correctRouter(path.join('/'));


                    //  一层单独处理
                    function menuInited ( data, index ) {
                        return data.map ( function ( item ,i) {
                            var len = item.list;
                            var classSelected = function(){
                                var match = (path[index] === item.name) || (i === 0 && !path[index]) || (item.jump && pathURL == layui.admin.correctRouter(item.jump||'')) || item.spread ;
                                if(match){
                                    admin.tabText= item.title;
                                    return len.length ? 'layui-nav-itemed' : 'layui-this';
                                }
                                return '';
                            };
                            var url = (item.jump && typeof item.jump === 'string') ? item.jump : item.name;
                            var child = [];
                            if( len && len.length > 0){
                                var nameArr = !!props.routerName ?[props.routerName,item.name]:[item.name];
                                child = itemChild(len,nameArr,30);
                            }
                            var isNewWindow = function(){
                                return item.isNewOpen=='1'?('target="_blank" href="'+serverPath.systemPath+layui.setter.openPath + url+'"'):('href="javascript:;"'+(item.jump?'lay-href="'+item.jump+'"':''));
                            };
                            return '<li data-name="'+(item.name)+'" data-jump="'+(item.jump ||url|| '') +'" class="layui-nav-item '+classSelected()+'"><a '+ isNewWindow() +' lay-tips="'+ item.title +'" lay-direction="2"><i class=" '+item.iconSkin+'"></i><cite>'+item.title+'</cite>' + (child.length>0?'<span class="layui-nav-more"></span>':'') + '</a>'+(child.length>0?'<dl class="layui-nav-child">'+child.join('')+'</dl>':'')+'</li>';
                        });
                    }

                    //  子集逻辑递归start
                    function itemChild (itemData,nameArr,pLeft) {
                        var paddingLeft = props.type === '1' ? 45 :( pLeft + 15);
                        return itemData.map(function(item,index){
                            var isPath = true;
                            var list = item.list;
                            var num = nameArr.length;
                            var newNameList = [];
                            var child = [];

                            // 路由分层处理
                            for(var i = 0 ; i < num; i++){
                                if(nameArr[i] !== path[i]){
                                    isPath = false;
                                }
                                newNameList.push(nameArr[i]);
                            }

                            newNameList.push(item.name);
                            if(item.name !== path[num]){
                                isPath = false;
                            }
                            var classSelected = function(){
                                var match = isPath || (item.jump && pathURL === layui.admin.correctRouter(item.jump)) || item.spread;
                                if(match){
                                    admin.tabText= item.title;
                                    return list && list.length ? 'layui-nav-itemed' : 'layui-this';
                                }
                                return '';
                            };
                            var url = (item.jump && typeof item.jump === 'string') ? item.jump : newNameList.join('/');
                            if(list && list.length>0){
                                child = itemChild(list,newNameList,paddingLeft);
                            }
                            var isNewWindow = function(){
                                return item.isNewOpen=='1'?('target="_blank" href="'+serverPath.systemPath+layui.setter.openPath + url+'"'):('href="javascript:;"'+(item.jump?'lay-href="'+item.jump+'"':''));
                            };
                            return '<dd data-name="'+ (item.name || '') +'" data-jump="' + ( item.jump || url||'') +'"'+ (classSelected() ? (' class="'+ classSelected() +'"') : '' )+'><a '+(item.iframe ? 'lay-iframe="true"' : '')+' style="padding-left:'+paddingLeft+'px"'+isNewWindow() +'><i class=" '+item.iconSkin+'"></i><cite>'+ item.title +'</cite>' +(child.length>0?'<span class="layui-nav-more"></span>':'')+'</a>'+(child.length>0?'<dl class="layui-nav-child">'+child.join('')+'</dl>':'')+'</dd>';
                        });

                    }


                    var menuTree = menuInited(props.result,0);
                    var MENUNAME = _this.publicquery.props.navLeftName;
                    var SELETESTSTE = 'layui-nav-itemed';
                    if(menuTree.length>0){
                        $(MENUNAME).html(menuTree.join(''));
                    }
                     element.render();
                    $(MENUNAME).find('li').removeClass(SELETESTSTE);
                    $(MENUNAME).find('dd').removeClass(SELETESTSTE);
                    if( props.type === '1' ){
                        $(MENUNAME+' li').mouseover(function(){
                            $(this).addClass(SELETESTSTE);
                            $(this).mouseout(function(){
                                $(this).removeClass(SELETESTSTE);
                            });
                        });
                        $(MENUNAME+' dd').mouseover(function(){
                            $(this).addClass(SELETESTSTE);
                            $(this).mouseout(function(){
                                $(this).removeClass(SELETESTSTE);
                            });
                        });
                    }else{
                        //  默认展开选择项
                        $(MENUNAME).find('.layui-this').parents('dd').addClass(SELETESTSTE);
                        $(MENUNAME).find('.layui-this').parents('.layui-nav-item').addClass(SELETESTSTE);
                    }



                });

            },
            topMenu:function(props){
                var name = this.publicquery.props.navTopName;
                var type = props.type;
                if(!!name){
                    if(type === '3'){
                        $(name+' li').hover(function(){
                            $(this).children("ul").show();
                        },function(){
                            $(this).children("ul").hide();
                        });
                    }else if(type === '4'){
                        $(name+' li a').click(function(){
                            $(name+' li ul').hide();
                            $(this).parents('ul').show();
                            if($(this).parent().children("ul").css('display') === 'none'){
                                $(this).parent().children("ul").show();
                                $(this).parent().hover(function(){
                                    $(this).children("ul").hide();
                                });
                            }else{
                                $(this).parent().find("ul").hide();
                            }
                        });


                    }

                }
                if(!!props.type){
                    this.publicquery.csslink({
                        type:type
                    });
                }
            },

            topMenuData: function(props){
                var _this = this;
                if(props.result){
                    var result = props.result.data;
                    layui.use(['admin'], function() {
                        var admin=layui.admin;
                        var path = layui.router().path
                            , pathURL = layui.admin.correctRouter(path.join('/'));
                        var listData = function (data) {
                            return data.map(function (item) {
                                var jump = item.jump;
                                var classSelected = function(){
                                    var match = (jump && pathURL == layui.admin.correctRouter(jump||''));
                                    if(match){
                                        admin.tabText= item.title;
                                        return item.list.length ? 'layui-nav-itemed' : 'layui-this';
                                    }
                                    return '';
                                };
                                if (jump && jump.substr(0, 1) !== '/') {
                                    jump = '/' + jump;
                                }
                                var childData = [];
                                if (item.list && item.list.length > 0) {
                                    childData = listChildData(item.list);
                                }
                                var isNewWindow = function(){
                                    return item.isNewOpen=='1'?('target="_blank" href="'+serverPath.systemPath+layui.setter.openPath + jump+'"'):(jump?('href="#'+jump+'" lay-href="'+jump+'"'):'');
                                };
                                return '<li data-name="'+(item.name)+'" data-jump="'+(item.jump|| '')+'" class="layui-nav-item  '+classSelected()+'"><a ' + isNewWindow() + '><cite>' + item.title + '</cite></a>' + (item.list && item.list.length > 0 ? ('<ul class="layui-nav-child ccpui-nav-child">' + childData.join('') + '</ul>') : '') + '</li>';
                            });
                        };
                        var listChildData = function (data) {
                            return data.map(function (item) {
                                var jump = item.jump;
                                var classSelected = function(){
                                    var match = (jump && pathURL == layui.admin.correctRouter(jump||''));
                                    if(match){
                                        admin.tabText= item.title;
                                        return item.list.length ? 'layui-nav-itemed' : 'layui-this';
                                    }
                                    return '';
                                };
                                if (jump && jump.substr(0, 1) !== '/') {
                                    jump = '/' + jump;
                                }
                                var childData = [];
                                if (item.list && item.list.length > 0) {
                                    childData = listChildData(item.list);
                                }
                                var isNewWindow = function(){
                                    return item.isNewOpen=='1'?('target="_blank" href="'+serverPath.systemPath+layui.setter.openPath + jump+'"'):(jump?('href="#'+jump+'" lay-href="'+jump+'"'):'');
                                };
                                return '<li data-name="'+(item.name)+'" data-jump="'+(item.jump|| '')+'" class="'+classSelected()+'"><a ' +isNewWindow()+'><cite>' + item.title +'</cite>'+ (item.list && item.list.length > 0 ? ('<span class="ccpui-nav-more"></span>') : '') + '</a>' + (item.list.length > 0 ? ('<ul class="layui-nav-child ccpui-nav-child">' + childData.join('') + '</ul>') : '') + '</li>';
                            });
                        };

                        $(_this.publicquery.props.navTopName).html(listData(result).join(''));
                        // 顶部菜单操作
                        _this.topMenu({
                            type: props.type
                        });
                    })
                }

            },

            // 顶部左侧init
            musterDataInit: function(props){
                var _this = this;
                var path = layui.router().href || null;

                if(props.result){
                    var result = props.result.data;

                    // 递归检索
                    var ind = 0;
                    var searchData = function (data,oldIndex){
                        data.forEach(function(item,index){
                            var jump = item.jump;
                            if(jump && jump.substr(0,1) !=='/'){
                                jump = '/'+ jump;
                            }
                            if(jump === path){
                                ind = ((oldIndex===0 || oldIndex) ? oldIndex : index);
                            }else if(item.list){
                                searchData(item.list,oldIndex || index);
                            }
                        });
                    };
                    searchData(result);

                    // top menu init
                    var topData = function(data){
                        return data.map(function(item,index){
                            var jump = item.jump;
                            if(jump && jump.substr(0,1) !=='/'){
                                jump = '/'+ jump;
                            }
                            var isNewWindow = function(){
                                return item.isNewOpen=='1'?('target="_blank" href="'+serverPath.systemPath+layui.setter.openPath + jump+'"'):(jump?('lay-href="'+jump+'"'):'');
                            };
                            return '<li class="layui-nav-item '+(index === ind ?'layui-this':'')+'"><a ind="'+index+'" '+isNewWindow()+'>'+item.title+'</a></li>';
                        });
                    };
                    $('#'+_this.publicquery.props.musterTopName).html(topData(result).join(''));
                    element.render();
                    // // 左侧菜单初始化
                    if(result[ind].list){
                        _this.navInit({
                            type:props.type,
                            result:result[ind].list
                        });
                    }
                    element.on('nav('+_this.publicquery.props.musterTopName+')', function (elem) {
                        if(result[elem.attr('ind')].list){
                            _this.navInit({
                                type:props.type,
                                result:result[elem.attr('ind')].list
                            });
                        }
                    });

                }
            },
            // menu调用
            menuInit: function(props){
                var _this = this;
                $.ajax({
                    url: props.url,
                    type: "GET",
                    async:false,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function(result) {
                        if(props.type === '0' || props.type === '1'){
                            // 左侧菜单初始化
                            _this.navInit({
                                type:props.type,
                                result:result.data
                            });
                        } else if(props.type === '3' || props.type === '4'){

                            // 顶部菜单init
                            _this.topMenuData({
                                result: result,
                                type:props.type
                            });

                        }else if(props.type === '2'){
                            _this.musterDataInit({
                                result: result,
                                type:props.type
                            });
                        }

                        if(!!props.type){
                            _this.publicquery.csslink({
                                type:props.type
                            });
                        }

                    }
                })
            }

        }
    //对外暴露的接口
    exports('initData', initData);
});